$(function(){
		cart_list();
		$("body").delegate(".input-add","click",function(event){
			var input_num=$(this).siblings('.input-num');
			var num=input_num.val();
			var newnum=Number(num)+1;
			
			var goods_number=input_num.attr('data-goods-number');
			sendbefore()
			$.post('/cart/edit_cart', {goods_number:goods_number,num:num,type:1,
			}, function(result) {
				if(result.code==0){
					input_num.val(num);
					layer.open({
					    content: result.message
					    ,skin: 'msg'
					    ,time: 2 //2秒后自动关闭
					  });
				
				}else{
					input_num.val(newnum);
				}
				sendafter()
			}, "json");
		})
		$("body").delegate(".input-sub","click",function(event){
			var input_num=$(this).siblings('.input-num');
			var num=input_num.val();
			var newnum=Number(num)-1;
			if(newnum<=0){
               layer.open({
					    content: '已经最少了'
					    ,skin: 'msg'
					    ,time: 2 //2秒后自动关闭
					  });
                return;
				var newnum=1;
			}
			sendbefore()
			var goods_number=input_num.attr('data-goods-number');
			$.post('/cart/edit_cart', {goods_number:goods_number,num:num,type:2,
			}, function(result) {
				if(result.code==0){
					input_num.val(num);
					layer.open({
					    content: result.message
					    ,skin: 'msg'
					    ,time: 2 //2秒后自动关闭
					  });
				}else{
					input_num.val(newnum);
				}
				sendafter()
			}, "json");
		});
		// 商品选择框
		$("body").on('click', '.check-wrapper', function() {
		
			if(!$(this).is(':checked')){
				$(this).attr("checked",false);
			}else{
				$(this).attr("checked",true);
			}
			
			var store_id=$(this).attr('store_id');
			var flag=true;
			$('.store_'+store_id+' .item-list .f_checkbox').each(function(key,value){
				if(!$(value).is(':checked')){
					flag=false;
				}
			})
			sendbefore();
			if(flag){
				$('.store_'+store_id+' .shopmain_title .f_checkbox').attr("checked",true);
				$('.store_'+store_id+' .shopmain_title .f_checkbox').addClass("checked");
			}else{
				$('.store_'+store_id+' .shopmain_title .f_checkbox').attr("checked",false);
			}
			select();
		})

		// 店铺选择框
		$("body").on('click', '.f_pub_checkbox', function() {
			var store_id=$(this).attr('store_id');
		
			if($(this).is(':checked')){
				var c_checked=1;
				$('.store_'+store_id+' .item-list .f_checkbox').attr("checked",true);
				$('.store_'+store_id+' .item-list .f_checkbox').addClass("checked");
			}else{
				var c_checked=0;
				$('.store_'+store_id+' .item-list .f_checkbox').removeAttr("checked");
				$('.store_'+store_id+' .item-list .f_checkbox').removeClass("checked");
			}
			sendbefore();
			$.post('/cart/store_select', {store_id:store_id,checked:c_checked
			}, function(result) {
				cart_list();
			}, "json");
		})

		
	
		// 删除
		$("body").on('click', '.del', function() {
			var cart_ids = $(this).attr('data');
			if (!cart_ids) {
				cart_ids = [];
				$("#cart_list").find(":checkbox:checked").each(function() {
					cart_ids.push($(this).val());
				})
			}
            //询问框
            layer.open({
                content: '您确定要从购物车中移除选中的商品吗?'
                ,btn: ['确定', '不要']
                ,yes: function(index){
                    sendbefore();
                    $.post('/cart/del_cart', {
                        data_id: cart_ids,
                    }, function(result) {
                        if(result.code==1){
                            cart_list();
                        }
                    }, "json");
                    layer.close(index);
                }
            });
		
			
		});
		
		// 结算
		$("body").on('click', '.submit-btn', function() {
			$.post('/cart/pay_sub', {}, function(result) {
				if(result.code==1){
					self.location='/pay/paymentOrder'; 
				}else{
					self.location='/user/login'; 
				}
			}, "json");
	});
		
})
function chkAll_onclick(){

	if($('.quanxuan .check-wrapper .cart-checkbox1').hasClass('checked')){
		$('.quanxuan .check-wrapper .cart-checkbox1').removeClass('checked');
		$('input[type|=checkbox]').removeAttr('checked'); 
	}else{
		$('.quanxuan .check-wrapper .cart-checkbox1').addClass('checked');
		$('input[type|=checkbox]').attr('checked','checked'); 
	}
	//select();
}
// 选择商品事件
function select() {
	cart_ids = [];
	$("#cart_list").find(":checkbox:checked").each(function() {
		cart_ids.push($(this).val());
	})	
	sendbefore();
	$.post('/cart/count_cart', {
		data_id: cart_ids,
	}, function(result) {
		cart_list();
	}, "json");
}
function sendbefore(){
	 $('.SZY-CART-SUBMIT-LOADING').show();
	 $('.xm-button').hide();
}
function sendafter(){
	 $('.SZY-CART-SUBMIT-LOADING').hide();
	 $('.xm-button').show();
}
function cart_list(){
	$.ajax({
	    url: '/cart/cart_list',
	    type: 'POST',
	    data: {data:1},
	    dataType: 'JSON',
	   	beforeSend:function(){
	      sendbefore();
	    },
		success:function(result) {
			sendafter();
				if(result.code==1){
					shop_info=result.data;
					$('.count_num').html(result.message.num);
					$('.SZY-CART-SELECT-GOODS-NUMBER').html(result.message.num);
					if(result.message.price<0){
						result.message.price=0.00;
					}
					$('#cart_amount_desc').html('<em>总计：</em>￥'+result.message.price);
					var content='<div class="folw_shopmain">';
					
					$.each(shop_info, function(key, val) {
						var q_select=1;
						$.each(val, function(k, v) {
							if(v.select==0){
								q_select=0;
							}
						});
						
						if(q_select==1){
							var q_select=' checked="checked"';
						}
						content+='<div class="store store_'+key+'" store_id="'+key+'"><div class="shopmain_title"><dl><input type="checkbox" '+q_select+' class="f_checkbox f_pub_checkbox f_pub_checkbox_'+key+'" store_id="'+key+'" style="margin-top:10px; margin-left:10px;"><dt><a class="shopLink eclipse" href="/store/index/id/'+key+'">'+val[0].store_name+'</a></dt><dd><a class="shopLink edit_btn" href="javascript:void(0)" name="'+key+'">编辑</a></dd></dl></div>';
						$.each(val, function(k, v) {
							if(v.select==1){
								var qu_select='select';
								var qu_select2='checked="checked"';
							}else{
								var qu_select='';
								var qu_select2='';
							}
							content+='<div class="item-list"><div class="inner"><input type="checkbox" store_id="'+key+'" autocomplete="off" name="sel_cartgoods[]" value="'+v.id+'" id="sel_cartgoods_'+v.cart_id+'" '+qu_select2+' class="f_checkbox check-wrapper check-wrapper-1"><div class="item_img"><a href="'+v.url+'"> <img src="'+v.shop_picture+'"></a></div><div class="goods_desc edit_info_'+key+'"> <dl><dt><a href="'+v.url+'">'+v.shop_name+'</a></dt>';
							if(v.sku_spec||v.sku_style){
						 		content+='<dd class="sku-line">';
						 		if(v.sku_spec){
						 			content+='规格：'+v.sku_spec+'&nbsp;&nbsp;';
						 		}
						 		if(v.sku_style){
						 			content+='颜色：'+v.sku_style;
						 		}
						 		content+='</dd>'
						 	}
							if(v.daiyan_sale>0){
								var sale_price='<em style="color: #f00;font-size: 12px;float: left;margin-left: 6px;">抵扣券 '+v.daiyan_sale+'</em>';
						 	}else{
						 		var sale_price='';
						 	}
						 	
						 	
						 	content+='</dl><div class="price"><span>￥'+v.price+'</span><i>￥'+v.oprice+'</i>'+sale_price+'<em>x'+v.num+' </em></div></div><div class="num edit_box_'+key+'" style="display:none;"><div class="qiehuan"><div class="xm-input-number"><a href="javascript:;" class="input-sub"></a> <input type="text"  class="input-num" disabled value="'+v.num+'" data-goods-number="'+v.id+'"> <a href="javascript:;"  class="input-add"></a> </div></div>  <div class="delete"> <a href="javascript:;" class="del" data="'+v.id+'"> 删除</a> </div></div> </div></div>';
						})
						 
						content+='</div>';
						
					});
					
					$('#cart_list').html(content);
					$('.flow_bottom').show();
				}
				
				if(result.code==0){
					$('#content .content').html('');
					var content='<section id="cart-content"><div class="qb_tac" style="padding:50px 0"> <img src="/themes/wnsc_moblie/newMobile/Cart/images/empty_cart.png" width="100" height="95"><br>购物车还是空的</div><div class="qb_gap" style="width:60%; margin:0 auto;"> <a href="/" class="mod_btn btn_strong">马上逛逛</a></div></section>';
					$('.flow_bottom').hide();
					$('#cart_list').html(content);
				}
		}
	});


}



